//
//  CouponViewController.m
//  LoveFreshBee
//
//  Created by brice Mac on 2016/10/20.
//  Copyright © 2016年 王忠迪. All rights reserved.
//
#define reuseId @"MyAddressCellId"

#import "CouponViewController.h"
#import "SVProgressHUD.h"
#import "RulesViewController.h"


@interface CouponViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *imgArray;

@end

@implementation CouponViewController

- (NSArray *)imgArray {
    if (_imgArray == nil) {
        NSMutableArray *nmArr = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(-10, 0, kScreenW+20, 150)];
            [nmArr addObject:img];
        }
        _imgArray = nmArr;
    }
    return _imgArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCouponVc];
    
}

- (void)setupCouponVc {
    
    //右侧跳转按钮
    UIBarButtonItem* rules = [[UIBarButtonItem alloc]initWithTitle:@"使用规则" style:UIBarButtonItemStyleDone target:self action:@selector(usingRules:)];
    [rules setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateNormal];
    self.navItem.rightBarButtonItem = rules;
    
    
}

/**
  跳到使用规则页面
 */
- (void)usingRules:(UIButton *)sender {
    
    RulesViewController *rule = [[RulesViewController alloc]init];
    rule.navItem.title = @"使用规则";
    [self.navigationController pushViewController:rule animated:YES];
}

//设置tableView
- (void)setupTableView {
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseId];
    
    self.tableView = tableView;
    self.tableView.rowHeight = 150;
    [self.view addSubview:tableView];
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //顶部搜索框栏
    UIView *searchBar = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, 40)];
    searchBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:searchBar];
    
    //输入框
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, kScreenW-120, 30)];
    textField.placeholder = @"请输入优惠券号码";
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 3, 20)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    textField.font = [UIFont fontWithName:@"Arial" size:15.0f];
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    
    textField.backgroundColor = [UIColor whiteColor];
    
    [searchBar addSubview:textField];
    
    //绑定按钮
    UIButton *binding = [[UIButton alloc]initWithFrame:CGRectMake(kScreenW-90, 7, 70, 26)];
    binding.layer.cornerRadius = 5;
    binding.clipsToBounds = YES;
    [binding setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    [binding setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_selected"] forState:UIControlStateSelected];
    
    [binding setTitle:@"绑定" forState:UIControlStateNormal];
    [binding.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [binding setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [binding addTarget:self action:@selector(bindingButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [searchBar addSubview:binding];
}

//绑定按钮点击事件
- (void)bindingButtonClick {
    
    [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:@"请输入正确的优惠券"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        UIImageView *img = self.imgArray[indexPath.row];
        img.image = [UIImage imageNamed:@"unusedCoupon"];
        [cell.contentView addSubview:img];
        
    }else {
        
        UIImageView *imageView = self.imgArray[indexPath.row];
        imageView.image = [UIImage imageNamed:@"usedCoupon"];
        [cell.contentView addSubview:imageView];
    }
    
    return cell;
}



@end
